package scheduledtasks.hander;


import scheduledtasks.ParseCorn;

import org.springframework.beans.factory.annotation.Autowired;
import scheduledtasks.entity.TaskObj;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;


/**
 * 【主入口 ]
 *
 * @author cxw
 * @version 1.0
 * @company bc
 * @copyright (c) 2022 bcInc. All rights reserved.
 * @date 2022-11-08 16:00:26
 * @since JDK1.8
 */

public class MainHander {


    @Autowired
    private SqlHander sqlHander;

    @Autowired
    private ParseCorn parseCorn;

    public   static  List<TaskObj> copyOnWriteArrayList=new CopyOnWriteArrayList<>();


    @PostConstruct
    public  void init(){
        new Thread(()->{a();}).start();
        new Thread(()->{cachae();}).start();
    }




    public void cachae(){

           try {
               copyOnWriteArrayList=sqlHander.connection("select * from scheduled");
               System.out.println(copyOnWriteArrayList);
           }catch (Exception e) {


           }
       }















    public    void  a(){
        try {
           while (true){
               try {
                   for (TaskObj t:copyOnWriteArrayList) {
                       new Thread(()->{
                           try {
                               if(null !=t.getSync() && t.getSync()==0 &&  parseCorn.parse(t) ){
                                   t.setSync(1);
                                   SpringArawaHander.springInvokeMethod(t.getClassName(),t.getMethoName(),null);
                                   t.setSync(0);
                                   t.setTime(System.currentTimeMillis());
                               }
                           }catch (Exception e){
                                  e.printStackTrace();
                           }
                       }).start();
                   }

               }catch (Exception e){
                   e.printStackTrace();
               }
           }
       }catch (Exception e){
           e.printStackTrace();

       }

    }
}
